home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Swar / ships.c < prev    next >
Text File  |  1994-08-21  |  6KB  |  214 lines

  1.  
  2. #include "swar.h"
  3.  
  4. #define MAX_SPEED    3
  5.  
  6. InitShips()
  7. {
  8.     extern RGBColor    myWhite, myBlack, myRed, myBlue, myYellow, myGreen, myOrange, myGray, myDkBlue;
  9.     extern SHIPREC        gShipRecs[MAX_PLAYERS], gOldShipRecs[MAX_PLAYERS];
  10.     extern Rect            gShipSrcRects[MAX_PLAYERS][8][8];
  11.     short i, j;
  12.     
  13.     for (i = 0; i < MAX_PLAYERS; i++) {
  14.         gShipRecs[i].where.v = RngRnd(25, 455);
  15.         gShipRecs[i].where.h = RngRnd(25, 615);
  16.         if (i) {
  17.             gShipRecs[i].vel.v = RngRnd(-MAX_SPEED, MAX_SPEED);
  18.             gShipRecs[i].vel.h = RngRnd(-MAX_SPEED, MAX_SPEED);
  19.         } /* if */
  20.         else {
  21.             gShipRecs[i].vel.v = 0;
  22.             gShipRecs[i].vel.h = 0;
  23.         } /* else */
  24.         gShipRecs[i].dir = RngRnd(0, 7);
  25.         gShipRecs[i].anim = RngRnd(0, 7);
  26.         gShipRecs[i].isAlive = TRUE;
  27.         gShipRecs[i].isAccel = FALSE;
  28.     } /* for */    
  29.     
  30.     gShipRecs[0].color = myBlue;
  31.     gShipRecs[1].color = myRed;
  32.     gShipRecs[2].color = myYellow;
  33.     gShipRecs[3].color = myGreen;
  34.  
  35.     for (i = 0; i < 8; i++)
  36.         for (j = 0; j < 8; j++) {
  37.             SetRect(&(gShipSrcRects[0][i][j]), 4 + (20 * j), 12 + (20 * i), 19 + (20 * j), 27 + (20 * i));
  38.             SetRect(&(gShipSrcRects[1][i][j]), 164 + (20 * j), 12 + (20 * i), 179 + (20 * j), 27 + (20 * i));
  39.             SetRect(&(gShipSrcRects[2][i][j]), 4 + (20 * j), 172 + (20 * i), 19 + (20 * j), 187 + (20 * i));
  40.             SetRect(&(gShipSrcRects[3][i][j]), 164 + (20 * j), 172 + (20 * i), 179 + (20 * j), 187 + (20 * i));
  41.         } /* for */
  42.     
  43. } /* InitShips() */
  44.  
  45. MoveShips()
  46. {
  47.     short i;
  48.     
  49.     for (i = 0; i < MAX_PLAYERS; i++)
  50.         if (gShipRecs[i].isAlive) {
  51.             if (gShipRecs[i].isAccel)
  52.                 switch (gShipRecs[i].dir) {
  53.                     case 0: gShipRecs[i].vel.v -= 1; break;
  54.                     case 1: gShipRecs[i].vel.v -= 1; gShipRecs[i].vel.h += 1; break;
  55.                     case 2: gShipRecs[i].vel.h += 1; break;
  56.                     case 3: gShipRecs[i].vel.v += 1; gShipRecs[i].vel.h += 1; break;
  57.                     case 4: gShipRecs[i].vel.v += 1; break;
  58.                     case 5: gShipRecs[i].vel.v += 1; gShipRecs[i].vel.h -= 1; break;
  59.                     case 6: gShipRecs[i].vel.h -= 1; break;
  60.                     case 7: gShipRecs[i].vel.v -= 1; gShipRecs[i].vel.h -= 1; break;
  61.                 } /* switch */
  62.             if (gShipRecs[i].vel.h > MAX_SPEED)
  63.                 gShipRecs[i].vel.h = MAX_SPEED;
  64.             if (gShipRecs[i].vel.h < -MAX_SPEED)
  65.                 gShipRecs[i].vel.h = -MAX_SPEED;
  66.             if (gShipRecs[i].vel.v > MAX_SPEED)
  67.                 gShipRecs[i].vel.v = MAX_SPEED;
  68.             if (gShipRecs[i].vel.v < -MAX_SPEED)
  69.                 gShipRecs[i].vel.v = -MAX_SPEED;
  70.             gShipRecs[i].where.h += gShipRecs[i].vel.h;
  71.             gShipRecs[i].where.v += gShipRecs[i].vel.v;
  72.             if (gShipRecs[i].where.h > 624) {
  73.                 gShipRecs[i].vel.h *= -1;
  74.                 gShipRecs[i].where.h = 624;
  75.             } /* if */
  76.             if (gShipRecs[i].where.h < 0) {
  77.                 gShipRecs[i].vel.h *= -1;
  78.                 gShipRecs[i].where.h = 0;
  79.             } /* if */
  80.             if (gShipRecs[i].where.v > 464) {
  81.                 gShipRecs[i].vel.v *= -1;
  82.                 gShipRecs[i].where.v = 464;
  83.             } /* if */
  84.             if (gShipRecs[i].where.v < 20) {
  85.                 gShipRecs[i].vel.v *= -1;
  86.                 gShipRecs[i].where.v = 20;
  87.             } /* if */
  88.             if (gShipRecs[i].anim < 0)
  89.                 gShipRecs[i].anim = 0;
  90.             if (++gShipRecs[i].anim > 7)
  91.                 gShipRecs[i].anim = 0;
  92.         } /* if */
  93.     
  94. } /* MoveShips() */
  95.  
  96. DrawShips()
  97. {
  98.     Rect        dstRect;
  99.     extern CGrafPort    *gScrapPtr, *gOSPtr;
  100.     short            i, j;
  101.     extern RGBColor    myWhite, myBlack;
  102.     
  103.     for (i = 0; i < MAX_PLAYERS; i++)
  104.         if (gShipRecs[i].isAlive) {
  105.             dstRect.top = gOldShipRecs[i].where.v;
  106.             dstRect.bottom = dstRect.top + 15;
  107.             dstRect.left = gOldShipRecs[i].where.h;
  108.             dstRect.right = dstRect.left + 15;
  109.             RGBForeColor(&myBlack);
  110.             RGBBackColor(&myBlack);
  111.             EraseRect(&dstRect);
  112.             RGBForeColor(&myBlack);
  113.             RGBBackColor(&myWhite);
  114.             dstRect.top = gShipRecs[i].where.v;
  115.             dstRect.bottom = dstRect.top + 15;
  116.             dstRect.left = gShipRecs[i].where.h;
  117.             dstRect.right = dstRect.left + 15;
  118.             CopyBits((BitMap *) *(gScrapPtr->portPixMap),
  119.                        (BitMap *) *(gOSPtr->portPixMap),
  120.                        &(gShipSrcRects[i][gShipRecs[i].dir][gShipRecs[i].anim]), &dstRect, srcCopy, 0L);
  121.             for (j = 0; j < MAX_PLAYERS; j++)
  122.                 if ((i != j) && CheckForShipCollision(j, dstRect)) {
  123.                     KillPlayer(i);
  124.                     KillPlayer(j);
  125.                 } /* if */
  126.             gOldShipRecs[i] = gShipRecs[i];
  127.         } /* if */
  128.  
  129. } /* DrawShips() */
  130.  
  131. CheckForShipCollision(s, r)
  132.     short    s;
  133.     Rect    r;
  134. {
  135.     Rect                    shipRect, tmpRect;
  136.     
  137.     if (gShipRecs[s].isAlive) {
  138.         shipRect.top = gShipRecs[s].where.v;
  139.         shipRect.bottom = shipRect.top + 15;
  140.         shipRect.left = gShipRecs[s].where.h;
  141.         shipRect.right = shipRect.left + 15;
  142.         if (SectRect(&r, &shipRect, &tmpRect))
  143.             return(1);
  144.     } /* if */
  145.     
  146.     return(0);
  147.     
  148. } /* CheckForShipCollision() */
  149.  
  150. KillPlayer(i)
  151.     short i;
  152. {
  153.     extern Handle        gBoomSoundH;
  154.     extern Boolean        gMatchIsEnding;
  155.     extern short        gMatchTicker;
  156.     extern SHIPREC        gShipRecs[MAX_PLAYERS];
  157.     short j;
  158.     Rect                dstRect;
  159.     
  160.     gShipRecs[i].isAlive = FALSE;
  161.             
  162.     dstRect.top = gShipRecs[i].where.v;
  163.     dstRect.bottom = dstRect.top + 15;
  164.     dstRect.left = gShipRecs[i].where.h;
  165.     dstRect.right = dstRect.left + 15;
  166.     RGBForeColor(&myBlack);
  167.     RGBBackColor(&myBlack);
  168.     EraseRect(&dstRect);
  169.     RGBForeColor(&myBlack);
  170.     RGBBackColor(&myWhite);
  171.     dstRect.top = gOldShipRecs[i].where.v;
  172.     dstRect.bottom = dstRect.top + 15;
  173.     dstRect.left = gOldShipRecs[i].where.h;
  174.     dstRect.right = dstRect.left + 15;
  175.     RGBForeColor(&myBlack);
  176.     RGBBackColor(&myBlack);
  177.     EraseRect(&dstRect);
  178.     RGBForeColor(&myBlack);
  179.     RGBBackColor(&myWhite);
  180.     
  181.     CreateDebris(5, 25, gShipRecs[i].where.h, gShipRecs[i].where.v, gShipRecs[i].vel.h, gShipRecs[i].vel.v);
  182.     ASndPlay(gBoomSoundH);
  183.  
  184.     if (!i) {
  185.         gMatchIsEnding = TRUE;
  186.         gMatchTicker = 50;
  187.     } /* if */
  188.     else {
  189.         gShipRecs[i].where.v = RngRnd(25, 455);
  190.         gShipRecs[i].where.h = RngRnd(25, 615);
  191.         if (i) {
  192.             gShipRecs[i].vel.v = RngRnd(-MAX_SPEED, MAX_SPEED);
  193.             gShipRecs[i].vel.h = RngRnd(-MAX_SPEED, MAX_SPEED);
  194.         } /* if */
  195.         else {
  196.             gShipRecs[i].vel.v = 0;
  197.             gShipRecs[i].vel.h = 0;
  198.         } /* else */
  199.         gShipRecs[i].dir = RngRnd(0, 7);
  200.         gShipRecs[i].anim = RngRnd(0, 7);
  201.         gShipRecs[i].isAlive = TRUE;
  202.         gShipRecs[i].isAccel = FALSE;
  203.     } /* else */
  204.     
  205.     for (j = 1; j < MAX_PLAYERS; j++)
  206.         if (gShipRecs[j].isAlive)
  207.             return;
  208.  
  209.     gMatchIsEnding = TRUE;
  210.     gMatchTicker = 50;
  211.  
  212.     
  213. } /* KillPlayer() */
  214.